package edu.cdbi.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseManager {
	
	//資料庫連結的驅動程式
	private static String drive = "com.mysql.jdbc.Driver";

	//資料庫連結的位址
	private static String url = "jdbc:mysql://127.0.0.1/cdbiproject?useUnicode=true&characterEncoding=utf8";

	//登入資料庫的使用者名稱 
	private static String user = "testcdbi";

	//登入資料庫的密碼 
	private static String password = "testcdbi";
	
	private static DatabaseManager instance;

	private DatabaseManager() {
		try { 
		    Class.forName(drive); 
		} 
		catch(ClassNotFoundException e) { 
		    System.err.println("找不到驅動程式類別"); 
		} 
	}
	
	public static DatabaseManager getInstance(){
		if(instance == null){
			instance = new DatabaseManager();
		}
		return instance;
	}
	
	public Connection createConnection(){
		try {
			Connection connection = DriverManager.getConnection(url, user, password);
		    if(!connection.isClosed()){
		    	return connection;
		    }else {
		    	return null;
		    }
		} catch (SQLException e) {
			System.err.println("無法連上SQL伺服器");
			return null;
		}
	}
	

}
